<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
	<title>Internet pizzeria - New order</title>
	
	<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAGaVKsN7Fq_fEe0RJPADRCBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxR_HUEVtZQfC_2K48p4H3BMNKsMCQ"
      type="text/javascript"></script>
    <script src="functions.js"></script>
	<script type="text/javascript">

	
		var directions;
		var distanceHTML;
		var waypoints;
		var pointLatLng;
		var geocoder;
		
		function radioClick (number) {
		    var defaultAddress = document.getElementById ("defaultAddress");
	    	if (number==1){
		        defaultAddress.style.display = "block";
	        	anotherAddress.style.display = "none";
    		} else if (number==2){
	        	defaultAddress.style.display = "none";
        		anotherAddress.style.display = "block";
    		}
    		return true;
		}
		
		function load() {
			<c:forEach var="pizza" items="${pizzas}">
				document.getElementById("tablePizza${pizza.id}").innerHTML=${pizza.price}.toFixed(2);
			</c:forEach>
			<c:forEach var="drink" items="${drinks}">
				document.getElementById("tableDrink${drink.id}").innerHTML=${drink.price}.toFixed(2);
			</c:forEach>
		
			if (GBrowserIsCompatible()){
				directions = new GDirections(null, null);
				GEvent.addListener(directions,"error", function() { alert("Directions failed: code "+directions.getStatus().code+"\n(Invalid address or there is no way to this address)"); });
				GEvent.addListener(directions,"load", onGDirectionsLoad);
				
				geocoder = new GClientGeocoder(); 
			}
			radioClick(1);
			document.getElementById("radio1").checked = true;
		}
		
		function validateAddressAndSend(arg0) {
			var sthOrdered = false;
			<c:forEach var="pizza" items="${pizzas}">
				if (document.getElementById("select${pizza.id}").value==0){
				} else {
					sthOrdered = true;
				}
			</c:forEach>
			if (!sthOrdered){
				<c:forEach var="drink" items="${drinks}">
					if (document.getElementById("select${drink.id}").value==0){
					} else {
						sthOrdered = true;
					}
				</c:forEach>
			}
			if (!sthOrdered){
				alert("You must order at least one product");
			} else{
				if (GBrowserIsCompatible()){
					var waypoints = new Array();
					var streetAndNumber, city, country, zipCode;
					waypoints[0] = "${pizzeriaAddress}";
					if (arg0 == 1){
						streetAndNumber = "<%out.print(request.getAttribute("streetAndNumber"));%>";
						city = "<%out.print(request.getAttribute("city"));%>";
						country = "<%out.print(request.getAttribute("country"));%>";
						zipCode = "<%out.print(request.getAttribute("zipCode"));%>";
					} else if (arg0 == 2){
						streetAndNumber = document.getElementById("streetAndNumberInputId").value;
						city = document.getElementById("cityInputId").value;
						country = document.getElementById("country").value;
						zipCode = document.getElementById("zipCode").value;					
					}
					waypoints[1] = address(streetAndNumber, city, zipCode, country);

					givePointLatLng(geocoder, waypoints[1]);					
					directions.loadFromWaypoints(waypoints);
				}
			}
		}
		
		function givePointLatLng(geocoder, waypoint){
			geocoder.getLatLng(waypoint,function(point) {
    			if (point) {
    				pointLatLng = point;
    			} else {
    			}
    		});
    	}
		
		function onGDirectionsLoad() {    
			distance = directions.getDistance();
			if (distance.meters<=<%out.print((Float)(request.getAttribute("DeliveryMaximumDistanceFromPizzeria")));%>){
				document.getElementById("feeForDelivery").value=feeForDelivery(
					distance.meters,
					<%out.print((Float)(request.getAttribute("DeliveryFreeDistanceInMeters")));%>,
					<%out.print((Float)(request.getAttribute("DeliveryFeeIntervalInMeters")));%>,
					<%out.print((Float)(request.getAttribute("DeliveryFeeForEveryIntervalInEuro")));%>
				);
				
				document.getElementById("pointLat").value = pointLatLng.lat();
				document.getElementById("pointLng").value = pointLatLng.lng();				
				document.getElementById("orderForm").submit();
			} else {
				alert("This address is too far from pizzeria");
			}
		}
	</script>

</head>
<body onload="load()">	
	<%@ include file="navigationMenu.jsp" %>

	<h3>New order</h3>

	<form id="orderForm" method="post" action="newOrderConfirmation">
	<input type="hidden" id="pointLat" name="pointLat" value="">
	<input type="hidden" id="pointLng" name="pointLng" value="">
	
	<%
		Integer max_amount = (Integer)(request.getAttribute("max_amount"));
	%>
	<h4>Choose products which you want to order</h4>
	<table border="5">
	<tr>
		<th colspan="5">
			Choose Pizzas
		</th>
	</tr>
	<tr>
			<th width="100">Pizza number</td>
			<th width="100" bgcolor="DDDDDD">Pizza name</td>
			<th width="200">Pizza description</td>
			<th width="100" bgcolor="DDDDDD">Pizza price</td>
			<th width="100">Amount of Pizza</td>
	</tr>
		<input type="hidden" name="max_amount" value=<%out.print(max_amount.intValue());%>>
	<c:forEach var="pizza" items="${pizzas}">
		<tr>
			<td align="center">${pizza.id}</td>
			<td bgcolor="DDDDDD">${pizza.name}</td>
			<td>${pizza.description}</td>
			<td id="tablePizza${pizza.id}" align="right" bgcolor="DDDDDD"></td>
			<td align="right" >
		
			
				<select id="select${pizza.id}" name="select${pizza.id}">
			<%
				for (int i=0; i<=max_amount.intValue(); i++){
			%>
				<option value = <%out.print(i+">"+i+"</option>");}%>
				</select>
				<input type="hidden" name="pizzasIds" value=${pizza.id}>
			</td>	
	
		</tr>
	</c:forEach>
	<tr>
		<th colspan="5">
			Choose drinks
		</th>
	</tr>
	<tr>
			<th>Drink number</td>
			<th bgcolor="DDDDDD">Drink name</td>
			<th>Drink description</td>
			<th bgcolor="DDDDDD">Drink price</td>
			<th>Amount of Drink</td>
	</tr>
	<c:forEach var="drink" items="${drinks}">
		<tr>
			<td align="center">${drink.id}</td>
			<td bgcolor="DDDDDD">${drink.name}</td>
			<td>${drink.description}</td>
			<td id="tableDrink${drink.id}" align="right" bgcolor="DDDDDD"></td>
			<td align="right" >
				<select id="select${drink.id}" name="select${drink.id}">
			<%
				for (int i=0; i<=max_amount.intValue(); i++){
			%>
				<option value = <%out.print(i+">"+i+"</option>");}%>
				</select>
				<input type="hidden" name="drinksIds" value=${drink.id}>
			</td>	
	
		</tr>
	</c:forEach>
	</table>
	<h4>Choose address where you want to receive package</h4>
	<%if (((Boolean)request.getAttribute("isCustomer")).booleanValue()){%>
	  <input id="radio1" type="radio" name="group1" value="defaultAddress" onclick="return radioClick (1)">Default address</input><br>
	  <input type="radio" name="group1" value="anotherAddress" onclick="return radioClick (2)">Another address</input><br>
	  <div id="defaultAddress">
		<table>
		   <tr>
			<td>name:</td>
			<td><%out.print(request.getAttribute("name"));%></td>
		   </tr>
		   <tr>	
			<td>surname:</td>
			<td><%out.print(request.getAttribute("surname"));%></td>
		   </tr>
		   <tr>
			<td>street and number:</td>
			<td><%out.print(request.getAttribute("streetAndNumber"));%></td>
		   </tr>
		   <tr>
			<td>city:</td>
			<td><%out.print(request.getAttribute("city"));%></td>
		   </tr>
		   <tr>
			<td>zip code:</td>
			<td><%out.print(request.getAttribute("zipCode"));%></td>
		   </tr>
		   <tr>
			<td>country:</td>
			<td><%out.print(request.getAttribute("country"));%></td>
		   </tr>
		</table>
		<button type="button" onclick="validateAddressAndSend(1);">Order</button>
	  </div>
	  <div id="anotherAddress">
		<table>
		   <tr>
			<td>name:</td>
			<td><%out.print(request.getAttribute("name"));%></td>
		   </tr>
		   <tr>	
			<td>surname:</td>
			<td><%out.print(request.getAttribute("surname"));%></td>
		   </tr>
		   <tr>
		    <td>street and number:</td> 
		    <td><input id="streetAndNumberInputId" name="userStreetAndNumber" value="<%out.print(request.getAttribute("streetAndNumber"));%>"></td>
		   </tr>
		   <tr>
		   <td>city: </td>
			<td><input id="cityInputId" name="userCity" value="<%out.print(request.getAttribute("city"));%>"></td>
		   </tr>
		   <tr>
		   <tr>	
			<td>zip code:</td> 
			<td><input id="zipCode" name="userZipCode" value="<%out.print(request.getAttribute("zipCode"));%>"></td>
		   </tr>
		   <td>country:</td>
		    <% String propertyCountry = (String)(request.getAttribute("propertyCountry"));
		    if (propertyCountry.equals("") || propertyCountry.matches("\\s")){%>
		   <td><input id="country" name="userCountry" value="<%out.print(request.getAttribute("country"));%>"></td>
		    <%} else {%>
		   <td><%out.print(propertyCountry);%></td>
		   <input id="country" name = "userCountry" type="hidden" value="<%out.print(propertyCountry);%>" /> 
		    <%}%> 
		   </tr>
		</table>
		<button type="button" onclick="validateAddressAndSend(2);">Order</button>
	  </div>
	<%} else{%>
		You must log in as customer!
	<%}%>		
	<input id="feeForDelivery" type="hidden" name="feeForDelivery" value="0.0" />
	</form>
	
	
</body>
</html>
